/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dk.rofl.graphics;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author menderleit
 */
public abstract class GameImageIO {

    // Loads an image file into an image that is compatible with the current graphics configuration.
    public static BufferedImage read(String fileName) throws IOException {
        // Load the image.
        File f = new File(fileName);
        BufferedImage fImg = ImageIO.read(f);

        // Create a new image that is compatible with the current graphics configuration.
        GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
        BufferedImage bImg = gc.createCompatibleImage(fImg.getWidth(), fImg.getHeight(), BufferedImage.TRANSLUCENT);

        // Copy the loaded image into the compatible image.
        Graphics2D g = (Graphics2D) bImg.getGraphics();
        g.setComposite(AlphaComposite.Src);
        g.setColor(new Color(0, 0, 0, 0));
        g.fillRect(0, 0, bImg.getWidth(), bImg.getHeight());
        g.drawImage(fImg, null, 0, 0);
        g.dispose();
        
        // Return the compatible image.
        return bImg;
    }
}
